﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Article : System.Web.UI.Page
{
    protected string sTitle = string.Empty;
    protected ArticleItem article;
    protected CategoryItem catItem=new CategoryItem();
    protected string sTitleString;
    protected string sMoreNewsTitle;
    private NewsType pageType;

    protected void Page_Load(object sender, EventArgs e)
    {
        GetParameters();

        if (article.articleId != 0)
        {
            //Set title line
            catItem = Category.ItemById(article.catId);
            if (catItem.Ancestor != -1)
            {
                catItem = Category.ItemById(catItem.Ancestor);
            }
            if (article.isPopular)
            {
                catItem.cPageName = "popularnews";
                catItem.cTitle = "Популярные статьи о медицине Израиля";
                catItem.catId = 2;
            }
            
            // metadata keywords
            HtmlMeta meta = new HtmlMeta();
            meta.Name = "keywords";
            meta.Content = PageHeaderData.GetArticleKeywords((int)article.articleId);
            this.Header.Controls.Add(meta);
            // metadata description
            meta = new HtmlMeta();
            meta.Name = "description";
            meta.Content = article.articleFragment;
            this.Header.Controls.Add(meta);
            
            //catItem = Category.ItemById(article.catId);

            HyperLink hlToCat = new HyperLink();
            hlToCat.Text = catItem.cTitle;
            hlToCat.NavigateUrl = string.Format("{0}.aspx", catItem.cPageName);
            sMoreNewsTitle = "Еще новости медицины Израиля:";

            switch (catItem.cPageName.ToLower())
            {
                case "news":
                    ucHeader1.index = "2";
                    divTitle.Attributes.Add("class", "title1");
                    catItem.cTitle = "Новости медицины Израиля";
                    hlToCat.Text = "Последние новости медицины Израиля";
                    break;
                case "popularnews":
                    ucHeader1.index = "3";
                    divTitle.Attributes.Add("class", "title2");
                    sMoreNewsTitle = "Еще популярные статьи о медицине Израиля:";
                    break;

                case "tourism":
                    ucHeader1.index = "6";
                    divTitle.Attributes.Add("class", "title8");
                    sMoreNewsTitle = "Еще статьи о медицинском туризме:";
                    break;

                case "deadsea":
                    divTitleLink.Visible = false;
                    Page.Title = catItem.cTitle;
                    ucHeader1.index = "7";
                    divTitle.Attributes.Add("class", "title4");
                    sTitleString=string.Format("<h1>{0}</h1>",catItem.cTitle);
                    break;

                case "about":
                    divTitleLink.Visible = false;
                    Page.Title = catItem.cTitle;
                    ucHeader1.index = "8";
                    divTitle.Attributes.Add("class", "title3");
                    sTitleString = string.Format("<h1>{0}</h1>", catItem.cTitle);
                    break;

                case "hotels":
                    ucHeader1.index = "6";
                    divTitle.Attributes.Add("class", "title8");

                    hlToCat.Text = "Гостиницы Израиля";
                    hlToCat.NavigateUrl = "hotels.aspx";
                    //divTitleLink.Controls.Add(hlToCat);


                    Image imgSeparator = new Image();
                    imgSeparator.ImageUrl = "images/bullet4.gif";
                    divTitleLink.Controls.Add(imgSeparator);

                    CategoryItem HotelItem = Category.ItemById(article.catId);
                    HyperLink hlToSubCat = new HyperLink();
                    hlToSubCat.Text = HotelItem.cTitle;
                    hlToSubCat.NavigateUrl = string.Format("{0}-h.aspx", HttpUtility.UrlEncode(HotelItem.cPageName));
                    divTitleLink.Controls.Add(hlToSubCat);
                    
                    break;

            }
            divTitleLink.Controls.AddAt(0,hlToCat);

            sTitleString = string.Format("<p>{0}</p>", catItem.cTitle);

            sTitle = catItem.cTitle;
            Page.Title = article.BrowserTitle;
            
            
            // hide date
            if (!article.isDateVisible)
                divDate.Visible = false;
            // set tags
            rptTags.DataSource = article._tags;
            if (article._tags==null||article._tags.Rows.Count == 0)
            {
                divTags.Visible = false;
            }
            else
                rptTags.DataBind();

            // Get news by theme
            Dal.arParams.Clear();
            Dal.arParams.Add(catItem.catId);//CatID
            Dal.arParams.Add(article.articleId);//ArticleID
            DataTable DT = Dal.ExecuteDBsp("usp_GetNewsByCategory", Dal.arParams);
            if (DT != null)
                if (DT.Rows.Count > 0)
                {
                    lblMoreNews.Visible = true;
                    gvNews.DataSource = DT;
                    gvNews.DataBind();
                }
        }
    }

    private void GetParameters()
    {
        string sFilename = Request.Params["a"];
        pageType= (NewsType)Convert.ToInt32(Request.Params["type"]);

        if (pageType>0)
        {
            article = Articles.GetArticleCatID((int)pageType); //o nas, Lechenie na mertvom more
        }
        else
        {
            article = Articles.GetArticleByName(sFilename);
        }
        
    }

}
